using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using Debug = UnityEngine.Debug;
using Random = UnityEngine.Random;

public static class BattleLoader
{
    public static BattleConfig battleConfig;
    public static PlayerData playerData;
    public static List<EnemyData> enemyData = new List<EnemyData>();
    static BattleLoader()
    {
        if (playerData == null)
        {
            CreateDefaultPlayerData();
        }
        battleConfig = new BattleConfig
        {
            enemyIds = new List<string> { "test1" },
            isBossBattle = false,
            isEliteBattle = false,
            nodeType = NodeType.Learning
        };
        Debug.Log("BattleLoader 静态类已初始化");
    }
    /// <summary>
    /// 创建默认的玩家数据
    /// </summary>
    private static void CreateDefaultPlayerData()
    {
        // 创建 GameObject 并添加 PlayerData 组件
        GameObject playerObj = new GameObject("DefaultPlayerData");
        playerData = playerObj.AddComponent<PlayerData>();

        // 确保 GameObject 在场景切换时不被销毁（可选）
        UnityEngine.Object.DontDestroyOnLoad(playerObj);

        // 确保玩家数据被正确初始化
        if (playerData != null)
        {
            Debug.Log("默认玩家数据创建成功");
        }
        else
        {
            Debug.LogError("默认玩家数据创建失败！");
        }
    }

    /// <summary>
    /// 设置玩家数据（从外部传入）
    /// </summary>
    public static void SetPlayerData(PlayerData newPlayerData)
    {
        if (newPlayerData == null)
        {
            Debug.LogError("无法设置空的玩家数据");
            return;
        }

        // 如果之前有玩家数据，销毁它
        if (playerData != null && playerData.gameObject != null)
        {
            GameObject.Destroy(playerData.gameObject);
        }

        playerData = newPlayerData;
        Debug.Log("BattleLoader 玩家数据已设置");
    }

    /// <summary>
    /// 安全的复制玩家数据到目标
    /// </summary>
    public static bool CopyPlayerDataTo(PlayerData target)
    {
        if (playerData == null)
        {
            Debug.LogError("BattleLoader.playerData 为 null，无法复制");
            return false;
        }

        if (target == null)
        {
            Debug.LogError("复制目标为 null");
            return false;
        }

        try
        {
            // 使用安全的复制方法
            playerData.DeepCopyTo(target);
            return true;
        }
        catch (System.Exception e)
        {
            Debug.LogError($"复制玩家数据失败: {e.Message}");
            return false;
        }
    }

}
